﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace GONES.Tools.Common
{
    public class SmtpMailClient
    {
        string _stmpServerUrl = "stmp.189.cn";
        string _sendMails;//用";"隔开的方式 82814722@qq.com;13365510155@189.cn;
        string _mailUser="pztservices@189.cn";
        string _mailPass="heyuqing";
        string _title;
        string _context;
        string _Error="";

        private void init(string mailAddrs, string title, string context)
        {
            _sendMails=mailAddrs;
            _title = title;
            _context = context;
        }

        public string SendMails
        {
            get { return _sendMails; }
            set { _sendMails = value; }
        }
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        public string Context
        {
            get { return _context; }
            set { _context = value; }
        }

        public string Error
        {
            get { return _Error; }
        }
        public  SmtpMailClient(string mailAddrs, string title,string context)
        {
            init(mailAddrs, title,context);
        }
        public  SmtpMailClient(string title, string context)
        {
            init("", title,context);
        }
        public bool Send()
        {
            if (string.IsNullOrEmpty(_sendMails) || string.IsNullOrEmpty(_title) || string.IsNullOrEmpty(_context))
            {
                _Error = "请设定邮件地址、标题和内容";
                return false;
            }
            string[] mailds = _sendMails.Split(new char[] { ';'}, StringSplitOptions.RemoveEmptyEntries);
            if (mailds.Length < 1)
            {
                _Error = "请设定邮件地址";
                return false;
            }

            MailMessage _mailMessage = new System.Net.Mail.MailMessage();
            for (int i = 0; i < mailds.Length - 1; i++)
            {
                _mailMessage.To.Add(mailds[i]);
            }
            _mailMessage.From = new MailAddress(_mailUser, "中冉客户", System.Text.Encoding.UTF8);
            _mailMessage.Subject = _title;//邮件标题 
            _mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
            _mailMessage.Body = _context;//邮件内容 
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
            _mailMessage.IsBodyHtml = false;//是否是HTML邮件 
            _mailMessage.Priority = MailPriority.High;//邮件优先级 

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(_mailUser, _mailPass);
            //在zj.com注册的邮箱和密码 
            client.Host = _stmpServerUrl;
            object userState = _mailMessage;
            try
            {
                client.SendAsync(_mailMessage, userState);
                //简单一点儿可以client.Send(msg); 
                _Error = "";
                return true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                _Error = ex.Message;
                return false;
            }
        }

        public bool send(string mailAddrs, string title, string context)
        {
            init(mailAddrs, title, context);
            return Send();
        }

    }
}
